package cn.rongdao.jrkabao.ui.deposit.account.uicomponent;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRCollection;

import java.util.ArrayList;
import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.deposit.MyAccountBean;
import cn.rongdao.jrkabao.ui.deposit.bill.BillActivity;
import cn.rongdao.jrkabao.util.CommonUtil;
import cn.rongdao.jrkabao.util.NumberUtil;
import cn.rongdao.jrkabao.widget.CustomPieChart;

/**
 * Created by jack on 2017/7/28
 */

public class MyAccountUIComponent extends LinearLayout {

    public MyAccountUIComponent(Context context) {
        super(context);
    }

    public MyAccountUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyAccountUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private CustomPieChart customPieChart;
    private ActivityUIComponent cashAdvanceActivityUIComponent;
    private ActivityUIComponent loanActivityUIComponent;
    private TextView tvTime;
    private TextView tvMoney;
    private TextView tvCashAdvanceActivity;
    private TextView tvLoanActivity;
    private TextView tvTotalMoney;

    private List<String> moneyList = new ArrayList<>();

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        customPieChart = (CustomPieChart) findViewById(R.id.lma_pie_chart);
        cashAdvanceActivityUIComponent = (ActivityUIComponent) findViewById(R.id.lma_ll_cash_advance);
        loanActivityUIComponent = (ActivityUIComponent) findViewById(R.id.lma_ll_loan);
        tvTime = (TextView) findViewById(R.id.lma_tv_time);
        tvMoney = (TextView) findViewById(R.id.lma_tv_money);
        tvTotalMoney = (TextView) findViewById(R.id.lma_tv_total_money);
        tvCashAdvanceActivity = (TextView) findViewById(R.id.lma_tv_cash_advance);
        tvLoanActivity = (TextView) findViewById(R.id.lma_tv_loan_activity);

        initListener();
    }

    private void initListener() {
        tvMoney.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                BillActivity.start(getContext());
            }
        });
    }

    public void setCustomPieChartContent(List<String> moneyList, boolean isHaveData) {
        customPieChart.setMoneyList(moneyList, isHaveData);
        customPieChart.startAnim();
    }

    @SuppressLint("SetTextI18n")
    public void setContent(MyAccountBean data) {
        setVisibility(View.VISIBLE);
        moneyList.clear();
        tvTime.setText(CommonUtil.getCurrentYearAndMonth()[1] + "月收益");
        tvTotalMoney.setText("+" + NumberUtil.getTwoDecimalPlaces(data.getT_total()));
        tvMoney.setText("+" + NumberUtil.getTwoDecimalPlaces(data.getM_total()));
        cashAdvanceActivityUIComponent.setCashAdvanceDrawable(NumberUtil.getTwoDecimalPlaces(data.getT_invitation()));
        moneyList.add(NumberUtil.getTwoDecimalPlaces(data.getT_activity()));
        loanActivityUIComponent.setActivityDrawable(NumberUtil.getTwoDecimalPlaces(data.getT_activity()));
        moneyList.add(NumberUtil.getTwoDecimalPlaces(data.getT_invitation()));
        setCustomPieChartContent(moneyList, Float.valueOf(data.getT_total()) > 0);
        tvCashAdvanceActivity.setText(getSpannableContent(FRCollection.appendAsString("取现活动\n", "+" + NumberUtil.getTwoDecimalPlaces(data.getM_invitation()), ""), 4));
        tvLoanActivity.setText(getSpannableContent(FRCollection.appendAsString("贷款活动\n", "+" + NumberUtil.getTwoDecimalPlaces(data.getM_activity()), ""), 4));
    }

    private SpannableString getSpannableContent(String content, int index) {
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(getContext(), R.color.c666666)), index, content.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        return ss;
    }

    public void stopPieChartAnim() {
        customPieChart.stopAnim();
    }
}
